package com.jieshuibao.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.model.db.H5Sign;
import com.jieshuibao.model.db.H5SignExample;
import com.jieshuibao.model.db.H5SignExample.Criteria;
import com.jieshuibao.service.H5SignService;
import com.jieshuibao.util.StringUtils;

@Controller
@RequestMapping("/api")
public class H5SignController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/h5Sign/list.do";
	
	@Resource
	private H5SignService h5SignService;
	
	@RequestMapping("/h5Sign/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		H5SignExample h5SignExample = new H5SignExample();
		int totalCount = h5SignService.countH5Sign(h5SignExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		h5SignExample.setPageIndex((pageNum - 1) * pageSize);
		h5SignExample.setPageSize(pageSize);
		List<H5Sign> h5Signs = h5SignService.getPageH5Sign(h5SignExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("h5Signs", h5Signs);
		return "/H5Sign/list";
	}
	
	@RequestMapping("/h5Sign/toAdd")
	public String toAdd(Model model) {
		return "/H5Sign/add";
	}

	@RequestMapping(value="/h5Sign/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(Model model, @RequestBody H5Sign h5Sign) {
		Map<String,String> res = new HashMap<String, String>();
		if(null == h5Sign){
			res.put("code", "1");
			res.put("des", "参数不能为空！");
			return res;
		}
		String phone = h5Sign.getPhone();
		if(StringUtils.isEmpty(phone)){
			res.put("code", "1");
			res.put("des", "手机号不能为空！");
			return res;
		}
		if(!StringUtils.isMobile(phone)){
			res.put("code", "1");
			res.put("des", "手机号格式错误！");
			return res;
		}
		if(StringUtils.isNotEmpty(phone)){
			H5SignExample example = new H5SignExample();
			Criteria createCriteria = example.createCriteria();
			createCriteria.andPhoneEqualTo(phone);
			List<H5Sign> list = h5SignService.selectByExample(example);
			if(null != list && list.size()>0){
				res.put("code", "1");
				res.put("des", "该手机号已报名！");
				return res;
			}
		}
		String email = h5Sign.getEmail();
		if(StringUtils.isNotEmpty(email)){
			if(!StringUtils.checkEmail(email)){
				res.put("code", "1");
				res.put("des", "邮箱格式错误！");
				return res;
			}
//			H5SignExample example2 = new H5SignExample();
//			Criteria createCriteria2 = example2.createCriteria();
//			createCriteria2.andEmailEqualTo(email);
//			List<H5Sign> list2 = h5SignService.selectByExample(example2);
//			if(null != list2 && list2.size()>0){
//				res.put("code", "1");
//				res.put("des", "该邮箱已报名！");
//				return res;
//			}
		}
		
		
		h5Sign.setSignTime(new Date());
		h5SignService.addH5Sign(h5Sign);
		res.put("code", "0");
		res.put("des", "报名成功！");
		return res;
	}

	@RequestMapping("/h5Sign/del")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		h5SignService.delH5SignById(id);
		return redirect;
	}

	@RequestMapping("/h5Sign/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		H5Sign h5Sign = h5SignService.getH5SignById(id);
		model.addAttribute("h5Sign", h5Sign);
		return "/H5Sign/edit";
	}

	@RequestMapping("/h5Sign/edit")
	public String edit(Model model, @ModelAttribute("h5Sign") H5Sign h5Sign) {
		h5SignService.updateH5Sign(h5Sign);
		return redirect;
	}

	@RequestMapping("/h5Sign/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		H5Sign h5Sign = h5SignService.getH5SignById(id);
		model.addAttribute("h5Sign", h5Sign);
		return "/H5Sign/view";
	}
}
